package com.ly.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.ly.service.ShopUserService;
import com.system.comm.util.BaseUtils;
import com.system.common.pojo.BisShopUser;
import com.system.common.pojo.SysMenu;
import com.zs.service.BisBusOrderService;

@Controller
@RequestMapping("shopuser")
public class ShopUserController {

	@Autowired
	private ShopUserService shopService;
	@Autowired
	private BisBusOrderService bisBusOrderService;
	
	@RequestMapping("insert")
	public String Insert(){
		return "stage";
	}

	@RequestMapping("login")
	public String login(String acc, String pass, Model model,HttpSession session) {
		String password=BaseUtils.encrypt(pass, BaseUtils.PASSWORD_KEY);
		Map<String, Object> map = new HashMap<>();
		if (acc != null) {
			map.put("acc", acc);
		}
		if (password != null) {
			map.put("pass", password);
		}
		BisShopUser user=shopService.select(map);
		session.setAttribute("shopUser",user);
		model.addAttribute("shopUser", user);	
		if (user != null) {
			if (user.getUserType().equals("1")) {
				model.addAttribute("shopList", shopService.getShop(user.getShopUserId()));
			}else{
				BisShopUser shopUser=(BisShopUser)session.getAttribute("shopUser");
				List<Map<String, Object>> shopList=shopService.selectHome(shopUser.getShopId());
				model.addAttribute("shopList", shopList);	
			}							
			return "BisShopUser/ShowBisShop";
		}
		return "stage";
	}
	
	@RequestMapping("queryshop")
	public String queryshop(String shopid,HttpSession session,Model model){
		if (shopid!=null&&!shopid.equals("")) { 
			BisShopUser user=(BisShopUser) session.getAttribute("shopUser");
			List<SysMenu> sysMenu = shopService.selectByAccount(user.getAccount());
			model.addAttribute("sysMenu", sysMenu);
			session.setAttribute("shopId", shopid);
			user.setPassword(BaseUtils.decrypt(user.getPassword(), BaseUtils.PASSWORD_KEY));
			List<Map<String, Object>> OrderList=bisBusOrderService.queryByOrder(shopid);
			for (Map<String, Object> map : OrderList) {
			    model.addAttribute("busmoney", map.get("busmoney"));
			    model.addAttribute("busnum", map.get("busnum"));
		  }
		}
		return "index2";
	}
	
	public BisShopUser getUser(String acc,String pass){
		String password=BaseUtils.encrypt(pass, BaseUtils.PASSWORD_KEY);
		Map<String, Object> map = new HashMap<>();
		if (acc != null) {
			map.put("acc", acc);
		}
		if (password != null) {
			map.put("pass", password);
		}
		return shopService.select(map);
	}

}
